<?php
	session_start();

	if (!isset($_SESSION['login']) && !$_SESSION['login'] == true) {
		header ("Location: index.php");
	}

	include("LangHeader.php");

	include 'config.php';

	if(isset($_GET['ID']) && isset($_GET['mID']))
	{
		$id = $_GET['ID'];
		$mID = $_GET['mID'];
	}
	else
	{
		header ("Location: listusers.php");
	}
	
	if(isset($_SESSION['SeeHidden']))
		$seeHidden = $_SESSION['SeeHidden'];
	else
	{
		$seeHidden = 0;
		$_SESSION['SeeHidden'] = 0;
	}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<link rel="stylesheet" type="text/css" href="style.css" />
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script src="scripts/jquery.tablesorter.js"></script>
<script src="scripts/salesmakeover.js"></script>
<title>Sales Makeover</title>
</head>

<body>
	<div class="container_head">
		<?php include("header.html"); ?>
	</div>
	<div class="container">
		<div class="editCompanyHeadline tr" key="edit_Label">Kontaktuppgifter</div>
		<div class="content_edit" id="content_edit">
			
				<?php
					$stmt = $con->prepare('SELECT ID, Name, Login FROM User WHERE ID = ?');
					$stmt->bind_param('i', $mID);
					$stmt->execute();
					$managerresult = $stmt->get_result();
					$managerrow = $managerresult->fetch_assoc();

					echo "<form class=\"form_editManager\" style=\"height:135px;\" id=\"form_editManager\" method=\"post\" action=\"saveManager.php?ID=" . $id . "&mID=" . $managerrow["ID"] . "\" accept-charset=\"UTF-8\" onsubmit=\"SalesMakeover.disableBeforeUnload(); return SalesMakeover.validateEditManagerForm(" . $managerrow["ID"] . ");\">";
					
					echo "<div class=\"editCompanyLabel tr\" key=\"editManager_Name\">Managers Namn</div>";
					echo "<div style='width:1024px;height:50px;'><input style='float:left;' type='text' onchange=\"SalesMakeover.enableBeforeUnload();\" name='managername' onblur='if(this.value == \"\") { this.value = \"" . $managerrow['Name'] . "\"; }' value=\"" . $managerrow['Name'] . "\">";
					
					echo "<div style='float:right;'></div></div>";
					
					echo "<div class=\"editCompanyLabel  tr\" key=\"editManager_Email\">Managers E-post</div>";
					echo "<input style='float:left;' type='email' onchange=\"SalesMakeover.enableBeforeUnload();\" name='manageremail' onblur='if(this.value == \"\") { this.value = \"" . $managerrow['Login'] . "\"; }' value=\"" . $managerrow['Login'] . "\">";
					
					echo "<div style='float:right;'id=\"button\"><input class=\"button tr\" key=\"Save\" type=\"submit\" value=\"Spara\"></div>";
					echo "</form>";
					
					echo "<span><div style='width:100%;height:30px;'><div style='float:right;'><label for='seeHidden' class='tr' key='See_Hidden'></label><input type='checkbox' id='seeHidden' name='seeHidden' onclick='SalesMakeover.clickHidden(this);' /></div></div></span>";
					echo "<table id=\"myTable\" class=\"tablesorter\" >";
					echo "<thead>";
					echo "<tr>";
					echo "<th class=\"name tr\" style='padding-left:15px;' key=\"Name\"></th>";
					echo "<th class=\"email tr\" key=\"Email\"></th>";
					echo "<th style='width: 164px;'></th>";
					
					echo "<th style='width:90px;'></th>";
					if($seeHidden == "1") 
						echo "<th class=\"buttons tr\" key=\"Activate\" style=\"width:46px;\"></th>";
					else
						echo "<th class=\"buttons tr\" key=\"Activate\" style=\"width:40px;\"></th>";
					
					echo "<th class=\"buttons tr\" key=\"Remove\"></th>";
					echo "</tr>";
					echo "</thead>";
					echo "<tbody style='height: 200px;' id='tableHolder'>";
					if($seeHidden == 1)
						$stmt = $con->prepare('SELECT User.ID, User.Name, User.Login, User.Active,User.ResetKey AS Activated, Company.ID AS CompanyID, User.Hidden FROM User INNER JOIN Company ON User.CompanyID = Company.ID WHERE User.CompanyID = ? AND User.UserRoleID = 3');
					else
						$stmt = $con->prepare('SELECT User.ID, User.Name, User.Login, User.Active,User.ResetKey AS Activated, Company.ID AS CompanyID, User.Hidden FROM User INNER JOIN Company ON User.CompanyID = Company.ID WHERE User.CompanyID = ? AND User.UserRoleID = 3 AND User.Hidden = 0');
					$stmt->bind_param('i', $id);
					$stmt->execute();
					$employeeresult = $stmt->get_result();
				
					while($row = $employeeresult->fetch_assoc())
					{
						echo "<tr>";
						echo "<td class='name'style='padding-left:15px;'>";
						echo $row['Name'];
						echo "</td>";
						echo "<td class='email'>";
						echo $row['Login'];
						echo "</td>";
						echo "<td class='business'>";
						//echo $row['CompanyName'];
						echo "</td>";
						echo "<td style='width:70px;'>";
						//echo "<a href='#' ><div class='list_button_edit'   ></div></a>";
						echo "</td>";
						if($seeHidden == "1")
						{
							echo "<td class='buttons'>";
							if($row['Hidden'] == 1)
								echo "<a href='#' ><div class='list_button_hidden' id=\"text1\" onMouseOver=\"show(getText('ButtonHidden'))\" onMouseOut=\"hide()\"  onclick='SalesMakeover.ReShowRow(" . $row['ID'] . ", \"" . $row['Name'] . "\")' ></div></a>";
							echo "</td>";
						}
						else
						{
							if($row['Activated']!=null)
							{
								echo "<td class='buttons'>";
								echo "<div class='list_button_pending' id=\"text1\" onMouseOver=\"show(getText('ButtonYellow'))\" onMouseOut=\"hide()\" id=\"text1\" title=\"Ej aktiverat konto. Klicka för att skicka ett nytt aktiveringsmail.\" onclick=\"SalesMakeover.SendMail('" . $row['Login'] . "', '" . $row['Name'] . "', '" . $row['Activated'] . "')\" ><input type=\"hidden\" value=" . $row['Active'] . "></div>";
								echo "</td>";


							}
							else
							{
								switch($row['Active'])
								{
									case 0:
										echo "<td class='buttons'>";
										echo "<a href='#' ><div class='list_button_active' id=\"text1\" onMouseOver=\"show(getText('ButtonRed'))\" onMouseOut=\"hide()\" onclick='SalesMakeover.ChangeActive(" . $row['ID'] . ")' ></div></a>";
										echo "</td>";
									break;
									case 1:
										echo "<td class='buttons'>";
										echo "<a href='#' ><div class='list_button_active_on' id=\"text1\" onMouseOver=\"show(getText('ButtonGreen'))\" onMouseOut=\"hide()\" onclick='SalesMakeover.ChangeActive(" . $row['ID'] . ")' ></div></a>";
										echo "</td>";
									break;
								}
							}
						}
						echo "<td class='buttons' >";
						if($row['Hidden'] == 0)
							echo "<a href='#' ><div class='list_button_remove' onclick='SalesMakeover.DeleteRow(" . $row['ID'] . ", \"" . $row['Name'] . "\")'></div></a>";
						echo "</td>";
						echo "</tr>";
						
					}
					echo "</tbody>";
					echo "</table>";
					echo "<div><span><div class=\"button_black tr\" key=\"Back\" style=\"margin-top:10px\" onclick=\"javascript:location.href='editCompany.php?ID=" . $id . "'\"></span>Tillbaka</div></div>";
				?>
			
			
		</div>
	</div>
	<?php if(isset($_SESSION["Popup"]) && isset($_SESSION["MessageKey"])){ $message = $_SESSION['Popup'];$message2=$_SESSION['MessageKey']; echo "<script type='text/javascript'>setTimeout(function() { alert('$message '+getText('$message2')); }, 10.2);</script>"; unset($_SESSION['Popup']); unset($_SESSION['MessageKey']);} ?>
	<div class="Effect" id="ButtinInfo">Div 1 Content</div>

<footer>
<div class = 'WaterMark'>© 2014 Sales Makeover</div>
</footer>


</body>

</html>
<script type="text/javascript">



var cursorX;
var cursorY;
document.onmousemove = function(e){
    cursorX = e.pageX;
    cursorY = e.pageY;
}
function show(text) 
{
	var d = document.getElementById('ButtinInfo');
	
	
	d.style.visibility = "visible";
	d.style.color = "gray";
	d.innerHTML = text;
	
	d.style.left = (cursorX-d.offsetWidth*0.5)+'px';
	d.style.top = (cursorY-d.offsetHeight-35)+'px';
}
function hide()
{
	var d = document.getElementById('ButtinInfo');
	d.style.visibility = "hidden";
}



$(document).ready(function() 
{ 
	var showHidden = "<?php echo ( isset($_SESSION['SeeHidden']) && $_SESSION['SeeHidden'] != '') ? $_SESSION['SeeHidden'] : '';?>";
	if(showHidden == "1")
		$("#seeHidden").prop('checked', true);
		
	SalesMakeover.doResize();
	window.onresize = SalesMakeover.doResize;
	$("#myTable").tablesorter({
		widgets: ["zebra"],
		widgetOptions : {
			zebra : [ "even", "odd" ]
		}
	});

}); 

function addManager()
{
	var gab = document.createElement('div');
	gab.setAttribute('class', 'OVER');
	
	var companyID = "<?php echo ( isset( $_GET['ID'] ) && $_GET['ID'] != '') ? $_GET['ID'] : '';?>";
	gab.innerHTML=
	'<div class="overlay_editCompany_addmanager"><p class="overlay_text">Ny manager</p>' +
	'<form id="editCompany_addManager" method="post" action="adding.php?id=manager" accept-charset="UTF-8">'+
	'<input type="hidden" name="companyID" value="' + companyID + '">' +
	'<input type="text" id="newmanagername" name="newmanagername" onfocus="if(this.value == \'Namn\') { this.value = \'\'; }" onblur="if(this.value == \'\') { this.value = \'Namn\'; }" value="Namn">'+
	'<br/>'+
	'<input type="text" id="newmanageremail" name="newmanageremail" onfocus="if(this.value == \'E-post\') { this.value = \'\'; }" onblur="if(this.value == \'\') { this.value = \'E-post\'; }" value="E-post">'+
	'<div class="form_add_buttons">'+
	'<div class="form_add_left_align" onclick="javascript:location.href=\'editCompany.php?ID=' + companyID + '\'">'+
	'<input type="button" value="Avbryt">'+
	'</div>'+
	'<div class="form_add_right_align">'+
	'<input type="submit" value="Lägg till">'+
	'</div></div></form></div>';

			
	
	
	$(".container").append(gab);
	//$(".overlay").append(btncontainer);
};


</script>